<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">


<meta name="google-site-verification" content="E9deYnivN5MuHMuIfiMZZfS0alv-d_0UjcwjBL79lGU" />



<meta name="baidu-site-verification" content="iHYWJxscwD" />










<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="true" />








  <meta name="baidu-site-verification" content="true" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="学习笔记,量化投资,kaggle竞赛,深度学习,pytorch," />










<meta name="description" content="首先自己手撸一个，按照b站上的视频来。这是一个用神经网络解决二分类问题的简单例子，先用单层神经网络，然后用多层神经网络(深度学习了)。神经网络的核心是将输入的每个神经节的值乘以其权重，再进行一定变换(比如用sigmoid函数，即激活函数)，得到输出。这个过程叫前向传播(Forward propagation)，得到的输出结果与训练值对比，计算误差，并用梯度下降法计算纠正值，纠正初始的权重(这叫反向">
<meta property="og:type" content="article">
<meta property="og:title" content="量化投资学习笔记97——kaggle量化投资比赛记录6-深度学习模型">
<meta property="og:url" content="https://zwdnet.github.io/2021/01/16/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B097%E2%80%94%E2%80%94kaggle%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E6%AF%94%E8%B5%9B%E8%AE%B0%E5%BD%956-%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E6%A8%A1%E5%9E%8B/index.html">
<meta property="og:site_name" content="赵瑜敏的口腔医学专业学习博客">
<meta property="og:description" content="首先自己手撸一个，按照b站上的视频来。这是一个用神经网络解决二分类问题的简单例子，先用单层神经网络，然后用多层神经网络(深度学习了)。神经网络的核心是将输入的每个神经节的值乘以其权重，再进行一定变换(比如用sigmoid函数，即激活函数)，得到输出。这个过程叫前向传播(Forward propagation)，得到的输出结果与训练值对比，计算误差，并用梯度下降法计算纠正值，纠正初始的权重(这叫反向">
<meta property="og:locale">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg">
<meta property="article:published_time" content="2021-01-16T02:40:23.000Z">
<meta property="article:modified_time" content="2021-01-17T02:59:07.000Z">
<meta property="article:author" content="赵瑜敏">
<meta property="article:tag" content="学习笔记">
<meta property="article:tag" content="量化投资">
<meta property="article:tag" content="kaggle竞赛">
<meta property="article:tag" content="深度学习">
<meta property="article:tag" content="pytorch">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zwdnet.github.io/2021/01/16/量化投资学习笔记97——kaggle量化投资比赛记录6-深度学习模型/"/>





  <title>量化投资学习笔记97——kaggle量化投资比赛记录6-深度学习模型 | 赵瑜敏的口腔医学专业学习博客</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">赵瑜敏的口腔医学专业学习博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zwdnet.github.io/2021/01/16/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B097%E2%80%94%E2%80%94kaggle%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E6%AF%94%E8%B5%9B%E8%AE%B0%E5%BD%956-%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E6%A8%A1%E5%9E%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="赵瑜敏的口腔医学专业学习博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">量化投资学习笔记97——kaggle量化投资比赛记录6-深度学习模型</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-16T02:40:23+00:00">
                2021-01-16
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" itemprop="url" rel="index">
                    <span itemprop="name">量化投资</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.4k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>首先自己手撸一个，按照b站上的<a target="_blank" rel="noopener" href="https://b23.tv/srXty3">视频</a>来。<br>这是一个用神经网络解决二分类问题的简单例子，先用单层神经网络，然后用多层神经网络(深度学习了)。<br>神经网络的核心是将输入的每个神经节的值乘以其权重，再进行一定变换(比如用sigmoid函数，即激活函数)，得到输出。这个过程叫前向传播(Forward propagation)，得到的输出结果与训练值对比，计算误差，并用梯度下降法计算纠正值，纠正初始的权重(这叫反向传播，Back propagation)。如此循环多次，得到训练后的各个神经节的权重值，最后用这组权重值去预测。而多层神经网络在输入层和输出层之间有一层或多层隐藏层，每层有若干个神经节(隐藏层的层数和每层的神经节个数就是调参的对象)，前一层的输出就是后一层的输入，每一层都进行上述的前向传播和后向传播过程。机器学习的目的，就是确定这些权重系数。<br>根据视频用numpy实现了上述单层和多层神经网络，大致清楚了基础过程。接下来就尝试用pytorch框架实现这个过程。<br>用<a target="_blank" rel="noopener" href="https://towardsdatascience.com/understanding-pytorch-with-an-example-a-step-by-step-tutorial-81fc5f8c4e8e">这篇</a>跟着撸吧。（好像要科学上网……）<br>网上大多数pytorch都用图像方面的应用来做例子，比如识别手写数字等。这篇是用线性回归作为例子来写的，我觉得比较好。<br>把代码撸了一遍，使用pytorch的主要步骤是：<br>①把数据转化为张量tensor，还可以组装为Dataset，目的是可以使用DataLoader加载数据，可以分批加载。张量与numpy的n维数组的区别是前者可以在GPU里使用。注意设置需要的参数的requires_grad为True。<br>②创建模型，继承自torch.nn.Module。主要实现初始化参数和前向传播forward过程。<br>③设置超参数，如学习率，迭代次数等。<br>④设置损失函数类型，如nn.MSELoss等，根据模型类型选择合适的损失函数。<br>⑤设置优化器，如optim.SGD等。<br>⑥建立迭代循环，每次循环中依次完成下述步骤：<br>a.设置训练模式：model.train()<br>b.获取预测值：y_pred = model(x)<br>c.计算损失： loss = loss_fn(y, y_pred)<br>d.计算loss的梯度，后向传播过程：loss.backward()<br>e.更新参数、梯度置零（用优化器完成）：<br>optimizer.step()<br>optimizer.item()<br>f.返回损失值。<br>⑦迭代后获得结果的参数，即模型结果。<br>⑧应用建模结果的参数对新数据进行预测。<br>最主要的，计算损失、计算梯度、更新参数等都由pytorch操作。<br><a target="_blank" rel="noopener" href="https://github.com/zwdnet/JSMPwork/blob/main/test_pytorch.py">代码</a><br>下面就尝试用pytorch构建深度学习模型来解题。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn</span><br><span class="line"><span class="keyword">import</span> torch.optim <span class="keyword">as</span> optim</span><br><span class="line">device = torch.device(<span class="string">&#x27;cuda&#x27;</span> <span class="keyword">if</span> torch.cuda.is_available() <span class="keyword">else</span> <span class="string">&#x27;cpu&#x27;</span>)</span><br><span class="line"><span class="keyword">import</span> janestreet</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 工具函数，返回神经网络训练的每一步</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">make_train_step</span>(<span class="params">model, loss_fn, optimizer</span>):</span></span><br><span class="line">    <span class="comment"># 执行在循环中训练过程</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">train_step</span>(<span class="params">x, y</span>):</span></span><br><span class="line">        <span class="comment"># 设置训练模式</span></span><br><span class="line">        model.train()</span><br><span class="line">        <span class="comment"># 预测</span></span><br><span class="line">        yhat = model(x)</span><br><span class="line">        <span class="comment"># 计算损失</span></span><br><span class="line">        <span class="comment"># print(&quot;测试&quot;)</span></span><br><span class="line">        <span class="comment"># print(len(yhat), len(y))</span></span><br><span class="line">        yhat = yhat.squeeze(-<span class="number">1</span>)</span><br><span class="line">        loss = loss_fn(yhat, y)</span><br><span class="line">        <span class="comment"># 计算梯度</span></span><br><span class="line">        loss.backward()</span><br><span class="line">        <span class="comment"># 更新参数，梯度置零</span></span><br><span class="line">        optimizer.step()</span><br><span class="line">        optimizer.zero_grad()</span><br><span class="line">        <span class="comment"># 返回损失值</span></span><br><span class="line">        <span class="keyword">return</span> loss.item()</span><br><span class="line">        </span><br><span class="line">    <span class="comment"># 返回在训练循环中调用的函数</span></span><br><span class="line">    <span class="keyword">return</span> train_step</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 建模过程</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">modeling</span>(<span class="params">train</span>):</span></span><br><span class="line">    print(<span class="string">&quot;开始建模&quot;</span>)</span><br><span class="line">    </span><br><span class="line">    x_train = train.loc[:, train.columns.<span class="built_in">str</span>.contains(<span class="string">&#x27;feature&#x27;</span>)]</span><br><span class="line">    y_train = train.loc[:, <span class="string">&#x27;action&#x27;</span>]</span><br><span class="line">    </span><br><span class="line">    x_tensor = torch.from_numpy(x_train.values).<span class="built_in">float</span>().to(device)</span><br><span class="line">    y_tensor = torch.from_numpy(y_train.values).<span class="built_in">float</span>().to(device)</span><br><span class="line">    </span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Model</span>(<span class="params">nn.Module</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">            <span class="built_in">super</span>(Model, self).__init__()</span><br><span class="line">            self.linear1 = nn.Linear(<span class="number">130</span>, <span class="number">65</span>)</span><br><span class="line">            self.linear2 = nn.Linear(<span class="number">65</span>, <span class="number">30</span>)</span><br><span class="line">            self.linear3 = nn.Linear(<span class="number">30</span>, <span class="number">1</span>)</span><br><span class="line">            <span class="comment"># self.linear4 = nn.Linear(25, 10)</span></span><br><span class="line">            <span class="comment"># self.linear5 = nn.Linear(10, 1)</span></span><br><span class="line">            self.sigmoid = nn.Sigmoid()</span><br><span class="line">        </span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">forward</span>(<span class="params">self, x</span>):</span></span><br><span class="line">            x = self.sigmoid(self.linear1(x))</span><br><span class="line">            x = self.sigmoid(self.linear2(x))</span><br><span class="line">            x = self.sigmoid(self.linear3(x))</span><br><span class="line">            <span class="comment"># x = self.sigmoid(self.linear4(x))</span></span><br><span class="line">            <span class="comment"># x = self.sigmoid(self.linear5(x))</span></span><br><span class="line">            <span class="keyword">return</span> x</span><br><span class="line">            </span><br><span class="line">    model = Model().to(device)</span><br><span class="line">    print(model.state_dict())</span><br><span class="line">    <span class="comment"># 设置超参数</span></span><br><span class="line">    lr = <span class="number">1e-2</span></span><br><span class="line">    n_epochs = <span class="number">1000</span></span><br><span class="line">     </span><br><span class="line">    <span class="comment"># loss_fn = nn.BCELoss(size_average = False)</span></span><br><span class="line">    loss_fn = nn.MSELoss(reduction = <span class="string">&quot;mean&quot;</span>)</span><br><span class="line">    optimizer = optim.SGD(model.parameters(), lr = lr)</span><br><span class="line">    <span class="comment"># 创建训练器</span></span><br><span class="line">    train_step = make_train_step(model, loss_fn, optimizer)</span><br><span class="line">    losses = []</span><br><span class="line">    </span><br><span class="line">    print(<span class="string">&quot;开始训练&quot;</span>)</span><br><span class="line">    <span class="comment"># 进行训练</span></span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(n_epochs):</span><br><span class="line">        <span class="comment"># y_tensor = y_tensor.detach()</span></span><br><span class="line">        loss = train_step(x_tensor, y_tensor)</span><br><span class="line">        losses.append(loss)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> model</span><br><span class="line"></span><br><span class="line"><span class="comment"># 特征工程</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">featureEngineer</span>(<span class="params">data</span>):</span></span><br><span class="line">    data = data[data[<span class="string">&#x27;weight&#x27;</span>] != <span class="number">0</span>]</span><br><span class="line">    data = data.fillna(<span class="number">0.0</span>)</span><br><span class="line">    weight = data[<span class="string">&#x27;weight&#x27;</span>].values</span><br><span class="line">    resp = data[<span class="string">&#x27;resp&#x27;</span>].values</span><br><span class="line">    data[<span class="string">&#x27;action&#x27;</span>] = ((weight * resp) &gt; <span class="number">0</span>).astype(<span class="string">&#x27;int&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> data</span><br><span class="line"></span><br><span class="line"><span class="comment"># 进行预测，生成提交文件，分类版</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">predict_clf</span>(<span class="params">model</span>):</span></span><br><span class="line">    env = janestreet.make_env()</span><br><span class="line">    iter_test = env.iter_test()</span><br><span class="line">    <span class="keyword">for</span> (test_df, sample_prediction_df) <span class="keyword">in</span> iter_test:</span><br><span class="line">        <span class="keyword">if</span> test_df[<span class="string">&#x27;weight&#x27;</span>].item() &gt; <span class="number">0</span>:</span><br><span class="line">            X_test = test_df.loc[:, test_df.columns.<span class="built_in">str</span>.contains(<span class="string">&#x27;feature&#x27;</span>)]</span><br><span class="line">            X_test = X_test.fillna(<span class="number">0.0</span>)</span><br><span class="line">            X_test_tensor = torch.from_numpy(X_test.values).<span class="built_in">float</span>().to(device)</span><br><span class="line">            res = model(X_test_tensor)</span><br><span class="line">            <span class="keyword">if</span> res &gt;= <span class="number">0.5</span>:</span><br><span class="line">                y_preds = <span class="number">1</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                y_preds = <span class="number">0</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            y_preds = <span class="number">0</span></span><br><span class="line">        print(y_preds)</span><br><span class="line">        sample_prediction_df.action = y_preds</span><br><span class="line">        env.predict(sample_prediction_df)</span><br><span class="line">        </span><br><span class="line">    </span><br><span class="line">train = pd.read_csv(<span class="string">&quot;/kaggle/input/jane-street-market-prediction/train.csv&quot;</span>)</span><br><span class="line">train = featureEngineer(train)</span><br><span class="line"></span><br><span class="line">print(<span class="string">&quot;深度学习&quot;</span>)</span><br><span class="line">model = modeling(train)</span><br><span class="line"><span class="comment"># 进行预测和提交</span></span><br><span class="line">predict_clf(model)</span><br><span class="line">print(<span class="string">&quot;结束。&quot;</span>)</span><br></pre></td></tr></table></figure>
<p>提交了，0分……再研究一下吧。<br>试一下<a target="_blank" rel="noopener" href="https://tigeraus.gitee.io/doc-optuna-chinese-build">optuna</a><br>参考<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_26752765/article/details/108225744">这篇</a><br>用来找xgboost模型的参数，结果为learning_rate = 0.07, max_depth = 15。<br>用这个参数提交一次看看，结果是2892.153。看来还需要找更多的参数。先摆着吧，至少方法会了。<br>下次，打算从头学一下深度学习和pytorch，在kaggle上找到两篇文章。</p>
<p>我发文章的三个地方，欢迎大家在朋友圈等地方分享，欢迎点“在看”。<br>我的个人博客地址：<a href="https://zwdnet.github.io/">https://zwdnet.github.io</a><br>我的知乎文章地址： <a target="_blank" rel="noopener" href="https://www.zhihu.com/people/zhao-you-min/posts">https://www.zhihu.com/people/zhao-you-min/posts</a><br>我的微信个人订阅号：赵瑜敏的口腔医学学习园地</p>
<p><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg"></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>欢迎打赏！感谢支持！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/mm_facetoface_collect_qrcode_1542944836634.png" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/1542944857770.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"># 学习笔记</a>
          
            <a href="/tags/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" rel="tag"># 量化投资</a>
          
            <a href="/tags/kaggle%E7%AB%9E%E8%B5%9B/" rel="tag"># kaggle竞赛</a>
          
            <a href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/" rel="tag"># 深度学习</a>
          
            <a href="/tags/pytorch/" rel="tag"># pytorch</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/01/11/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B096%E2%80%94%E2%80%94kaggle%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E6%AF%94%E8%B5%9B%E8%AE%B0%E5%BD%955-%E6%A8%A1%E5%9E%8B%E9%9B%86%E6%88%90/" rel="next" title="量化投资学习笔记96——kaggle量化投资比赛记录5-模型集成">
                <i class="fa fa-chevron-left"></i> 量化投资学习笔记96——kaggle量化投资比赛记录5-模型集成
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/01/20/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B098%E2%80%94%E2%80%94kaggle%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E6%AF%94%E8%B5%9B%E8%AE%B0%E5%BD%957-%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%8F%8Apytorch/" rel="prev" title="量化投资学习笔记98——kaggle量化投资比赛记录7-深度学习及pytorch">
                量化投资学习笔记98——kaggle量化投资比赛记录7-深度学习及pytorch <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80MTA2Mi8xNzU4Nw=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">452</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">29</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">544</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">本站版权归赵瑜敏所有，如欲转载请与本人联系。</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">1225.8k</span>
  
</div>









<div>
  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1275447216'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s11.cnzz.com/z_stat.php%3Fid%3D1275447216%26online%3D1%26show%3Dline' type='text/javascript'%3E%3C/script%3E"));</script>
</div>

        







  <div style="display: none;">
    <script src="//s95.cnzz.com/z_stat.php?id=1275447216&web_id=1275447216" language="JavaScript"></script>
  </div>



        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  
</body>
</html>
